package com.virgo.demo.system.controller.admin;

import cn.hutool.core.lang.tree.Tree;
import com.virgo.common.entity.IResponse;
import com.virgo.demo.core.annotation.Log;
import com.virgo.demo.system.domain.bo.SystemResourceBo;
import com.virgo.demo.system.service.SystemResourceService;
import com.virgo.limit.annotation.RepeatSubmit;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/manage168/systemResource")
@RequiredArgsConstructor
public class SystemResourceController {

	private final SystemResourceService systemResourceService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@Log(module = "权限节点", type = "编辑", permission = "system_resource_save")
	@RepeatSubmit
	public IResponse<Void> saveAction(@Validated SystemResourceBo systemResourceBo) {
		systemResourceService.saveSystemResource(systemResourceBo);
		return IResponse.ok();
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@Log(module = "权限节点", type = "删除", permission = "system_resource_del")
	@RepeatSubmit
	public IResponse<Void> delAction(HttpServletRequest request) {
		String ids = IRequestUtils.getString(request,"ids","");
		if (ObjectUtils.isEmpty(ids)) {
			return IResponse.fail("参数错误");
		}
		systemResourceService.deleteSystemResource(ids);
		return IResponse.ok();
	}

	@RequestMapping(value = "/tree", method = RequestMethod.POST)
	@Log(permission = "system_resource_list", saveLog = false)
	public IResponse<List<Tree<Long>>> treeAction(HttpServletRequest request) {
		return IResponse.ok(systemResourceService.resourceTree(request));
	}
}